using Microsoft.EntityFrameworkCore;
using StudentManagement.Domain.Entities;
using StudentManagement.Domain.Repositories;
using StudentManagement.Infrastructure.Data;

namespace StudentManagement.Infrastructure.Repositories;

public class ScoreRepository : Repository<Score>, IScoreRepository
{
    public ScoreRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<IEnumerable<Score>> GetScoresByStudentIdAsync(int studentId)
    {
        return await _dbSet.Where(s => s.StudentId == studentId).ToListAsync();
    }

    public async Task<IEnumerable<Score>> GetScoresByCourseIdAsync(int courseId)
    {
        return await _dbSet.Where(s => s.CourseId == courseId).ToListAsync();
    }

    public async Task<Score> GetScoreByStudentAndCourseAsync(int studentId, int courseId)
    {
        return await _dbSet.FirstOrDefaultAsync(s => s.StudentId == studentId && s.CourseId == courseId);
    }

    public async Task<IEnumerable<Score>> GetScoresWithStudentAndCourseAsync()
    {
        return await _dbSet
            .Include(s => s.Student)
            .Include(s => s.Course)
            .ToListAsync();
    }

    public async Task<decimal?> GetAverageScoreByCourseIdAsync(int courseId)
    {
        return await _dbSet
            .Where(s => s.CourseId == courseId && s.TotalScore.HasValue)
            .AverageAsync(s => s.TotalScore);
    }
}